package com.example.hotel_demo.service;


import com.example.hotel_demo.mapper.BillMapper;
import com.example.hotel_demo.mapper.RoomTypeMapper;
import com.example.hotel_demo.pojo.Bill;
import com.example.hotel_demo.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class BillService {
    @Autowired
    private BillMapper billMapper;

    @Autowired
    private BookService bookService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private RoomTypeMapper roomTypeMapper;

    public int addBill(Book book){
        int billMoney = roomTypeMapper.getRoomPriceBYid(book.getTypeID());
        System.out.println(book.getRoomID()+","+book.getCustNum()+","+book.getArriveDate()+","+book.getLeaveDate()+","+100+","+billMoney+","+0);

        return billMapper.addBill(book.getRoomID(),book.getCustNum(),book.getArriveDate(),book.getLeaveDate(),100,billMoney,0 ,book.getBookID());
    }

    public int countallBill() {
        return billMapper.countallBil();
    }
    public int countForRoomRental() {
        return billMapper.countForRoomRental();
    }

    public int countForCustLiving() {
        return billMapper.countForCustLiving();
    }

    public Bill getbillBybookID(String bookID) {
        return billMapper.getbillBybookID(bookID);
    }

    public void checkCustNum(int custState, int billID, String bookID, String roomID, String custNum, String custName, String custPhone, String custNum2, String custName2, String custPhone2, String ddd, String deposit, String billMoney) {
        if(custName2 != null && custNum2!=null && custPhone2 != null){
            //判断是否存在cust2
            int flag2 = customerService.updateCustState(custNum2,roomID,custState);
            if(flag2 != 1){
                //cust表不存在custNum2，在customer上创建一个
                String sex = "男";
                customerService.addCust(custName2,custNum2,Integer.parseInt(custPhone2),sex,Integer.parseInt(roomID),custState);
            }
            //cust表存在cust2，就修改cust1
            int flag1 = customerService.updateCustState(custNum,roomID,custState);
            if (flag1 ==1){
                //修改book表中bookstate
                bookService.updateBookStateByBookId(2,Integer.parseInt(bookID));
                //是否交了押金，并收集到的资料update一下bill的custNum
                billMapper.updateBybillID(billID,roomID,custNum,custNum2,ddd,deposit,billMoney);
            }
        }else{
            int flag1 = customerService.updateCustState(custNum,roomID,custState);
            if (flag1 ==1){
                //修改book表中bookstate
                bookService.updateBookStateByBookId(2,Integer.parseInt(bookID));
                //是否交了押金，并收集到的资料update一下bill的custNum
                billMapper.updateBybillID(billID,roomID,custNum,"",ddd,deposit,billMoney);
            }
        }
    }

    public Bill getbillByRoomID(String roomID) {
       return billMapper.getbillByRoomID(roomID);
    }

    public Bill getbillBybillID(String billID) {
        return billMapper.getbillBybillID(billID);
    }

    public void updateBillState(String billID, String billState) {
         billMapper.updateBillState(billID,billState);
    }

    public void deleteBillByBookId(int bookID) {
        billMapper.deleteBillByBookId(bookID);
    }
}
